package com.chenwenxuan.sync.console.task;

import java.util.List;
import java.util.Map;

import com.chenwenxuan.sync.console.TaskInvoker;
import com.chenwenxuan.sync.console.model.Task;
import com.chenwenxuan.sync.console.model.TaskDebugInfo;
import com.chenwenxuan.sync.console.model.TaskRuntimeInfo;
import com.chenwenxuan.sync.console.model.TaskRuntimeInfo.ReturnStatus;
import com.chenwenxuan.sync.console.service.TaskInvokerService;

public abstract class AbstractExecuteTask implements ExecuteTask {

	protected Task task;
	protected Map<String, String> context;
	protected TaskInvoker invoker;
	protected TaskInvokerService service;
	
	protected Throwable t;
	protected int workCount = 0;
	protected List<TaskDebugInfo> debugInfos;
	protected String returnMessage;
	protected ReturnStatus returnStatus;
	
	
	public void work( int count ) {
		workCount += count;
	}
	
	public void work() {
		work(1);
	}
	
	public void returnStatus(String returnMessage, ReturnStatus returnStatus) {
		this.returnMessage = returnMessage;
		this.returnStatus = returnStatus;
	}
	
	@Override
	public TaskRuntimeInfo collectTaskRuntimeInfo() {
		TaskRuntimeInfo runtimeInfo = new TaskRuntimeInfo();
		runtimeInfo.setEnableDebugLog(task.isDebugEnable());
		runtimeInfo.setDebugLogs(this.debugInfos.toArray(new TaskDebugInfo[0]));
		runtimeInfo.setMsg(returnMessage);
		runtimeInfo.setRefTask(task);
		runtimeInfo.setRuntimeExcepton(t);
		runtimeInfo.setWorkCount(workCount);
		runtimeInfo.setStatus(returnStatus);
		return runtimeInfo;
	}
	
	
	@Override
	public void setTask(Task task) {
		this.task = task;
	}
	@Override
	public void setContext(Map<String, String> context) {
		this.context = context;
	}
	@Override
	public void setInvoker(TaskInvoker invoker) {
		this.invoker = invoker;
	}

	@Override
	public void setTaskService(TaskInvokerService service) {
		this.service = service;
	}

	@Override
	public Task getTask() {
		return this.task;
	}

	@Override
	public Map<String, String> getContext() {
		return this.context;
	}

	@Override
	public TaskInvoker getInvoker() {
		return this.invoker;
	}

	@Override
	public TaskInvokerService getTaskService() {
		return this.service;
	}
	
}
